<?php
error_reporting(E_ALL);
$lightheader = FALSE;
include('include/config.inc.php');
include('include/header.inc.php');

// QUERY FOR CALLID
$sql  = "SELECT * FROM cdr WHERE CallId = '" . $_GET['callid'] . "'";

// QUERY EXCEPTION
if(!$result = $db->query($sql)) { 
 	message_die(CRITICAL_ERROR, "Could not query config information in portal_board", "", __LINE__, __FILE__, $sql);
 	exit;
}
else
{
	echo '<br><br>';
	echo '<table cellpadding="0" cellspacing="0" border="0" width="90%" align="center">';
	echo ' <tr height="36">';
	echo '  <td width="26" background="images/tl-gry.gif">&nbsp;</td>';
	echo '  <td width="485" background="images/t-gry.gif" colspan="2"><b>Summary<b></td>';
	echo '  <td width="10" background="images/tr-gry.gif"></td>';
	echo ' </tr>';
	echo '</table>';
	
	echo '<table cellpadding="0" cellspacing="0" border="0" width="90%" align="center">';
	echo ' <tr height="10">';
	echo '  <td width="10" background="images/mt-l.gif">&nbsp;</td>';
	echo '  <td width="501" background="images/mt-c.gif" colspan="2">&nbsp;</td>';
	echo '  <td width="10" background="images/mt-r.gif">&nbsp;</td>';
	echo ' </tr>';
	echo '</table>';	
	echo '<table cellpadding="0" cellspacing="0" border="0" width="90%" align="center">';

	while ($row = $result->fetchArray(SQLITE3_ASSOC)) {
		
		// FROM - TO
		$from = NameAndNumber($row['OriginationName'], $row['OriginationNumber']);
		$to = NameAndNumber($row['CalledName'], $row['CalledNumber']);
		$dest = NameAndNumber($row['DestinationName'], $row['DestinationNumber']);
		
		// CHECK FOR CALL TRANSFER
		$transfer = "";
		$transferby = "";
		if ( $row['State']=="Transferred" && $row['DisconnectReason']=="CallTransferred" )
		{
			$sql_transfer = "SELECT * FROM cdr WHERE CallId = '" . $row['TransferredToCallId'] . "'";
			
			if(!$result_transfer = $db->query($sql_transfer)) { 
 				message_die(CRITICAL_ERROR, "Could not query config information in portal_board", "", __LINE__, __FILE__, $sql_transfer);
 				exit;
			}
			else
			{
				while ($row_transfer = $result_transfer->fetchArray(SQLITE3_ASSOC)) {
					$transfer_CallId = $row_transfer['CallId'];
					$transfer_to_number = $row_transfer['CalledNumber'];
					$transfer_to_name = $row_transfer['CalledName'];
					$transfer = '<a href="info.php?callid=' . $transfer_CallId . '">' . NameAndNumber($transfer_to_name, $transfer_to_number) . '</a>';
				}
			}
		}
		
		// CHECK FOR CALL TRANSFER BY
		if ( trim($row['TransferredByNumber'])!="" && trim($row['TransferredByName'])!="" )
		{
			$sql_transferby = "SELECT * FROM cdr WHERE CallId = '" . $row['TransferredCallId1'] . "'";
			
			if(!$result_transferby = $db->query($sql_transferby)) { 
 				message_die(CRITICAL_ERROR, "Could not query config information in portal_board", "", __LINE__, __FILE__, $sql_transferby);
 				exit;
			}
			else
			{
				while ($row_transferby = $result_transferby) {
					$transferby_CallId = $row_transferby['CallId'];
					$transfer_by_number = $row_transferby['OriginationNumber'];
					$transfer_by_name = $row_transferby['OriginationName'];
					$transferby = '<a href="info.php?callid=' . $transferby_CallId . '">' . NameAndNumber($transfer_by_name, $transfer_by_number) . '</a>';
				}
			}
		}		

		// CALCULATE TIME DIFFS
		if ( $row['ConnectTime']=="" || $row['StartTime']=="" )
		{
			$respondtime = $lang['noconnect'];
		}
		else
		{
			$respondtime = strtotime($row['ConnectTime'])-strtotime($row['StartTime']);
			$respondtimeminutes = floor($respondtime/60);
			$respondtimeseconds = $respondtime - ( $respondtimeminutes * 60 ) ;
			$respondtime = $respondtimeminutes . " minutes and " . $respondtimeseconds . " seconds";
		}
		
		// CHECK FOR CONNECT
		if ( $row['EndTime']=="" || $row['ConnectTime']=="" )
		{
			$callduration = $lang['noconnect'];
		}
		else
		{
			if ( $row['State']=="Transferred" && $row['DisconnectReason']=="CallTransferred" )
			{
				$callduration = strtotime($row['TransferTime'])-strtotime($row['ConnectTime']);
				$calldurationminutes = floor($callduration/60);
				$calldurationseconds = $callduration - ( $calldurationminutes * 60 ) ;
				$callduration = $calldurationminutes . " minutes and " . $calldurationseconds . " seconds";
			}
			else
			{
				$callduration = strtotime($row['EndTime'])-strtotime($row['ConnectTime']);
				$calldurationminutes = floor($callduration/60);
				$calldurationseconds = $callduration - ( $calldurationminutes * 60 ) ;
				$callduration = $calldurationminutes . " minutes and " . $calldurationseconds . " seconds";
			}
		}		

		// BEGIN TABEL CONTENTS
		echo '<tr>';
		echo ' <td width="10" background="images/mb-l.gif">&nbsp;</td>';
		echo ' <td style="border-bottom: 1px solid #000000;">' . $lang['from'] . '</td>';
		echo ' <td style="border-bottom: 1px solid #000000;">' . $from . '</td>';
		echo '  <td width="10" background="images/mb-r.gif">&nbsp;</td>';
		echo '</tr>';
		echo '<tr>';
		echo ' <td width="10" background="images/mb-l.gif">&nbsp;</td>';
		echo ' <td style="border-bottom: 1px solid #000000;">' . $lang['to'] . '</td>';
		echo ' <td style="border-bottom: 1px solid #000000;">' . $to . '</td>';
		echo ' <td width="10" background="images/mb-r.gif">&nbsp;</td>';
		echo '</tr>';
		echo '<tr>';
		echo ' <td width="10" background="images/mb-l.gif">&nbsp;</td>';
		echo ' <td style="border-bottom: 1px solid #000000;">' . $lang['dest'] . '</td>';
		echo ' <td style="border-bottom: 1px solid #000000;">' . $dest . '</td>';
		echo ' <td width="10" background="images/mb-r.gif">&nbsp;</td>';
		echo '</tr>';
		echo '<tr>';
		echo ' <td width="10" background="images/mb-l.gif">&nbsp;</td>';
		echo ' <td style="border-bottom: 1px solid #000000;">' . $lang['callstate'] . '</td>';
		echo ' <td style="border-bottom: 1px solid #000000;">' . $row['State'] . ' - ' . $row['DisconnectReason'] . '</td>';
		echo '  <td width="10" background="images/mb-r.gif">&nbsp;</td>';
		echo '</tr>';	
		// IF THIS CALL IS TRANSFERRED TO
		if ( $row['State']=="Transferred" && $row['DisconnectReason']=="CallTransferred" )
		{
			echo '<tr>';
			echo ' <td width="10" background="images/mb-l.gif">&nbsp;</td>';
			echo ' <td style="border-bottom: 1px solid #000000;">' . $lang['tranferredto'] . '</td>';
			echo ' <td style="border-bottom: 1px solid #000000;">' . $transfer . '</td>';
			echo ' <td width="10" background="images/mb-r.gif">&nbsp;</td>';
			echo '</tr>';		
		}
		// IF THIS CALL IS TRANSFERRED BY
		if ( trim($row['TransferredByNumber'])!="" && trim($row['TransferredByName'])!="" )
		{
			echo '<tr>';
			echo ' <td width="10" background="images/mb-l.gif">&nbsp;</td>';
			echo ' <td style="border-bottom: 1px solid #000000;">' . $lang['tranferredby'] . '</td>';
			echo ' <td style="border-bottom: 1px solid #000000;">' . $transferby . '</td>';
			echo ' <td width="10" background="images/mb-r.gif">&nbsp;</td>';
			echo '</tr>';		
		}
		echo '<tr>';
		echo ' <td width="10" background="images/mb-l.gif">&nbsp;</td>';
		echo ' <td style="border-bottom: 1px solid #000000;">' . $lang['starttime'] . '</td>';
		echo ' <td style="border-bottom: 1px solid #000000;">' . date('Y-m-d H:i:s', strtotime($row['StartTime'])) . '</td>';
		echo ' <td width="10" background="images/mb-r.gif">&nbsp;</td>';
		echo '</tr>';	
		if ( trim($row['ScriptConnectTime'])!="" )
		{
			echo '<tr>';
			echo ' <td width="10" background="images/mb-l.gif">&nbsp;</td>';
			echo ' <td style="border-bottom: 1px solid #000000;">' . $lang['ScriptConnectTime'] . '</td>';
			echo ' <td style="border-bottom: 1px solid #000000;">' . date('Y-m-d H:i:s', strtotime($row['ScriptConnectTime'])) . '</td>';
			echo ' <td width="10" background="images/mb-r.gif">&nbsp;</td>';
			echo '</tr>';		
		}
		if ( trim($row['DeliveredTime'])!="" )
		{
			echo '<tr>';
			echo ' <td width="10" background="images/mb-l.gif">&nbsp;</td>';
			echo ' <td style="border-bottom: 1px solid #000000;">' . $lang['DeliveredTime'] . '</td>';
			echo ' <td style="border-bottom: 1px solid #000000;">' . date('Y-m-d H:i:s', strtotime($row['DeliveredTime'])) . '</td>';
			echo ' <td width="10" background="images/mb-r.gif">&nbsp;</td>';
			echo '</tr>';		
		}
		if ( trim($row['ConnectTime'])!="" )
		{
			echo '<tr>';
			echo ' <td width="10" background="images/mb-l.gif">&nbsp;</td>';
			echo ' <td style="border-bottom: 1px solid #000000;">' . $lang['ConnectTime'] . '</td>';
			echo ' <td style="border-bottom: 1px solid #000000;">' . date('Y-m-d H:i:s', strtotime($row['ConnectTime'])) . '</td>';
			echo ' <td width="10" background="images/mb-r.gif">&nbsp;</td>';
			echo '</tr>';		
		}
		if ( trim($row['EndTime'])!="" )
		{
			echo '<tr>';
			echo ' <td width="10" background="images/mb-l.gif">&nbsp;</td>';
			echo ' <td style="border-bottom: 1px solid #000000;">' . $lang['EndTime'] . '</td>';
			echo ' <td style="border-bottom: 1px solid #000000;">' . date('Y-m-d H:i:s', strtotime($row['EndTime'])) . '</td>';
			echo ' <td width="10" background="images/mb-r.gif">&nbsp;</td>';
			echo '</tr>';		
		}
		if ( trim($row['TransferTime'])!="" )
		{
			echo '<tr>';
			echo ' <td width="10" background="images/mb-l.gif">&nbsp;</td>';
			echo ' <td style="border-bottom: 1px solid #000000;">' . $lang['TransferTime'] . '</td>';
			echo ' <td style="border-bottom: 1px solid #000000;">' . date('Y-m-d H:i:s', strtotime($row['TransferTime'])) . '</td>';
			echo ' <td width="10" background="images/mb-r.gif">&nbsp;</td>';
			echo '</tr>';		
		}
		echo '<tr>';
		echo ' <td width="10" background="images/mb-l.gif">&nbsp;</td>';
		echo ' <td style="border-bottom: 1px solid #000000;">' . $lang['respondtime'] . '</td>';
		echo ' <td style="border-bottom: 1px solid #000000;">' . $respondtime . '</td>';
		echo ' <td width="10" background="images/mb-r.gif">&nbsp;</td>';
		echo '</tr>';	
		echo '<tr>';
		echo ' <td width="10" background="images/mb-l.gif">&nbsp;</td>';
		echo ' <td style="border-bottom: 1px solid #000000;">' . $lang['callduration'] . '</td>';
		echo ' <td style="border-bottom: 1px solid #000000;">' . $callduration . '</td>';
		echo ' <td width="10" background="images/mb-r.gif">&nbsp;</td>';
		echo '</tr>';	
			

	}
	echo ' <tr height="10">';
 	echo '  <td width="10" background="images/b-l.gif">&nbsp;</td>';
 	echo '  <td width="300" background="images/b-c.gif" colspan="2">&nbsp;</td>';
 	echo '  <td width="10" background="images/b-r.gif">&nbsp;</td>';
	echo ' </tr>';
		echo '</table>';
}

include('include/footer.inc.php');
?>